#include "framework.h"
#include <QFileDialog>
#include <QDebug>
#include "Tools\ZLogManager.h"
#include "Scene\ZDataManager.h"
#include "Render\RenderSystem.h"
#include "Common\ZAppManager.h"
#include "MachineLearning\DPTest.h"
#include "MachineLearning\JointBoostingTest.h"
#include "Tools\ConfigManager.h"

Framework::Framework(QSettings* setting, QWidget *parent)
	: QMainWindow(parent)
{
	setSettings(setting);
	ui.setupUi(this);
    ui.qglviewerWidget->setRenderer(ZFramework::RenderSystem::getSingletonPtr()->getPointCloudRenderer());
}

Framework::~Framework()
{

}

void Framework::initSettings()
{

}

void Framework::setSettings(QSettings* settings)
{
	setting_ = settings;
}

void Framework::initSystem()
{
    //ZFramework::ZLogManager::getSingletonPtr();
}

void Framework::updateViews()
{
    ui.qglviewerWidget->updateGL();
}

void Framework::loadMesh(QString fileName)
{
    ZFramework::DataManager* manager = ZFramework::DataManager::getSingletonPtr();
    ZFramework::MeshSpace::PointCloud *pc = new ZFramework::MeshSpace::PointCloud();
    if (pc->loadFromFile(fileName.toLocal8Bit()))
    {
        manager->clear();
        manager->addPointCloud(pc);
    }
    updateViews();
}

void Framework::slot_save_mesh()
{}

void Framework::slot_load_mesh()
{
    DebugLog << "Begin to load mesh..";
    QString fileName = QFileDialog::getOpenFileName(this,
        tr("Open mesh file"),
        QDir::currentPath(),
        tr("OBJ File(*.obj);;"
        "All Files (*)"));
    if (!fileName.isEmpty())
        loadMesh(fileName);
    updateViews();
}

void Framework::slot_run_algorithm()
{
    DebugLog << "Begin to run algorithm!";
    DebugLog << "Begin to run algorithm!";
    //ZFramework::MachineLearningTest::DPTest::runTest2();
    QString fileName = QFileDialog::getOpenFileName(this,
        tr("Open data txt file"),
        QDir::currentPath(),
        tr("OBJ File(*.txt);;"
        "All Files (*)"));
    if (!fileName.isEmpty())
        ZFramework::MachineLearningTest::DPTest::runDPAlgorithm(fileName.toLocal8Bit());
    updateViews();
}

void Framework::slot_run_analyse()
{
    DebugLog << "Begin to analyse result!";
    DebugLog << "Begin to analyse result!";
    const std::string strLastFolder("lastFolder");
    ZFramework::Config* config = ZFramework::ConfigManager::getSingletonPtr()->getConfig();
    QString openFolder = QDir::currentPath();
    if (config->isKeyExists(strLastFolder))
    {
        std::string folder;
        folder = config->read(strLastFolder, folder);
        openFolder = QString::fromLocal8Bit(folder.c_str());
    }
    
    QFileDialog *fileDialog = new QFileDialog(this);
    fileDialog->setWindowTitle(tr("Open XML file"));
    fileDialog->setDirectory(openFolder);
    fileDialog->setNameFilter(tr("XML File(*.xml);;""All Files(*)"));
    if (fileDialog->exec() == QDialog::Accepted)
    {
        QString path = fileDialog->selectedFiles()[0];
        std::string folder(fileDialog->directory().canonicalPath().toLocal8Bit());
        config->add(strLastFolder, folder);
        ZFramework::MachineLearningTest::JBTest::run(path.toLocal8Bit());
    }

    //QString xmlFile = QFileDialog::getOpenFileName(this,
    //    tr("Open xml file"),
    //    QDir::currentPath(),
    //    tr("XML File(*.xml);;""All Files(*)"));
    //if (!xmlFile.isEmpty())
    //{
    //    ZFramework::MachineLearningTest::JBTest::run(xmlFile.toLocal8Bit());
    //}
    updateViews();
}

void Framework::slot_load_data()
{
    DebugLog << "Begin to read data!";
    const std::string strLastFolder("lastFolder");
    ZFramework::Config* config = ZFramework::ConfigManager::getSingletonPtr()->getConfig();
    QString openFolder = QDir::currentPath();
    if (config->isKeyExists(strLastFolder))
    {
        std::string folder;
        folder = config->read(strLastFolder, folder);
        openFolder = QString::fromLocal8Bit(folder.c_str());
    }

    QFileDialog *fileDialog = new QFileDialog(this);
    fileDialog->setWindowTitle(tr("Open XML file"));
    fileDialog->setDirectory(openFolder);
    fileDialog->setNameFilter(tr("XML File(*.xml);;""All Files(*)"));
    if (fileDialog->exec() == QDialog::Accepted)
    {
        QString path = fileDialog->selectedFiles()[0];
        std::string folder(fileDialog->directory().canonicalPath().toLocal8Bit());
        config->add(strLastFolder, folder);
        //ZFramework::MachineLearningTest::JBTest::run(path.toLocal8Bit());
    }
}